﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SuperJoe.Framework.UnitOfWorkSql;
using WWX.Admin.IDAL;
using WWX.Dto;
using WWX.Dto.Search;
using WWX.Model;

namespace WWX.Admin.DAL
{
    public class RechargeDAL : BaseDAL, IRechargeDAL
    {
        public RechargeDAL(IUnitOfWork uow) : base(uow)
        {
        }

        public void Add(Recharge recharge)
        {
            var sql = "insert into recharges(id, memberid,no,amount,zamount,date,remark,createdtime) values(?id, ?memberid,?no,?amount,?zamount,?date,?remark,?createdtime)";
            var parameters = new Dictionary<string, object>();
            parameters.Add("id", recharge.Id);
            parameters.Add("memberid", recharge.MemberId);
            parameters.Add("no", recharge.No);
            parameters.Add("amount", recharge.Amount);
            parameters.Add("zamount", recharge.ZAmount);
            parameters.Add("date", recharge.Date);
            parameters.Add("remark", recharge.Remark);
            parameters.Add("createdtime", recharge.CreatedTime);
            //parameters.Add("createdby", recharge.CreatedBy);

            this.Execute(sql, parameters);
        }

        public List<RechargeAdminDto> GetPageList(RechargeAdminSearch search, int pageNo, int pageSize, out int total)
        {
            List<RechargeAdminDto> retList = null;
            Dictionary<string, object> parameters = null;
            var whereSql = GenerateWhereSql(search, out parameters);

            string totalSql = "select count(1) from recharges r, members m where r.memberid=m.id " + whereSql;
            total = this.GetCount(totalSql, parameters);
            if (total != 0)
            {
                var pagingSql = "select r.*, m.account memberaccount,m.phone memberphone, m.nickname membernickname, m.truename membertruename from recharges r, members m where r.memberid=m.id " + whereSql + " order by date desc limit " + (pageNo - 1) * pageSize + "," + pageSize;

                retList = this.GetModels<RechargeAdminDto>(pagingSql, parameters);

            }

            return retList ?? new List<RechargeAdminDto>();
        }

        private string GenerateWhereSql(RechargeAdminSearch search, out Dictionary<string, object> parameters)
        {
            parameters = this.CreateParameters();
            string sql = "";

            if (search.EndDate.HasValue)
            {
                sql += " and r.date<'" + search.EndDate.Value.AddDays(1).Date.ToString() + "'";
            }

            if (search.StartDate.HasValue)
            {
                sql += " and r.date>='" + search.StartDate.Value.Date.ToString() + "'";
            }
            if (search.No.IsNotNullOrWhiteSpace())
            {
                sql += " and r.no like ?no";
                parameters.Add("no", $"%{search.No}%");
            }

            if (search.Remark.IsNotNullOrWhiteSpace())
            {
                sql += " and r.remark like ?remark";
                parameters.Add("remark", $"%{search.Remark}%");
            }

            if (search.MemberAccount.IsNotNullOrWhiteSpace())
            {
                sql += " and m.account like ?account";
                parameters.Add("account", $"%{search.MemberAccount}%");
            }

            if (search.MemberPhone.IsNotNullOrWhiteSpace())
            {
                sql += " and m.phone like ?phone";
                parameters.Add("phone", $"%{search.MemberPhone}%");
            }

            if (search.MemberTrueName.IsNotNullOrWhiteSpace())
            {
                sql += " and m.truename like ?truename";
                parameters.Add("truename", $"%{search.MemberTrueName}%");
            }

            if (search.MemberNickName.IsNotNullOrWhiteSpace())
            {
                sql += " and m.nickname like ?nickname";
                parameters.Add("nickname", $"%{search.MemberNickName}%");
            }


            return sql;
        }
    }
}
